{% extends "exploration/base.html" %}

{% block content %}
<script type="text/javascript" charset="utf-8">
// Django security issues
$('html').ajaxSend(function(event, xhr, settings) {
    function getCookie(name) {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
    if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
        // Only send the token to relative URLs i.e. locally.
        xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
    }
});
    
// Send to server primary key of open attribute implication we want to accept
// and then reload to main page
$(function() {
 $("div#open_attribute_implications .aimp_accept").click(function(e) {
   e.preventDefault();
   $.post('attributeimplication/accept', { pk: $(this).attr("id")},
   function(data) {
     document.location.href="/";
   });
 });
     
// Send to server primary key of open attribute implication we want to reject
$("div#confirmed_attribute_implications .aimp_reject").click(function(e) {
    e.preventDefault();
    $.post('attributeimplication/unaccept', { pk: $(this).attr("id")},
    function(data) {
      document.location.href="/";
    });
});
      
// Implication rejection
$("div#open_attribute_implications .aimp_reject").click(function(e) {
  $(this).parents("div.implication").addClass("highlight")
  e.preventDefault();
  var imp_pk = $(this).attr("id");
  // Get from server premise of rejected implication
  $.post('getpremise', { pk: imp_pk},
   function(data) {
     $("#context tr#new_object .new_obj_checkboxes").each(function(index) {
         if (data.indexOf($(this).attr("name")) != -1) {
             $(this).prop({
                 checked: true,
                 disabled: true
             });
         }
       });
     $("#hidden_imp_pk").attr("value", imp_pk);
     $("#context tr#new_object").addClass("highlight");
     $("#context tr#new_object").show();
     $("#context").scrollTop($("#context")[0].scrollHeight);
     $('body').animate({
                 scrollTop: 0
           }, 'slow');
   });
});

$("div#confirmed_attribute_implications").hide()
$("div#exploration h1#background_title").click(function() {
    $("div#confirmed_attribute_implications").slideToggle();
})

$("div#open_attribute_implications").hide()
$("div#exploration h1#open_title").click(function() {
    $("div#open_attribute_implications").slideToggle();
})

if ($.cookie('filter') == "Concepts") {
    $('select#filter_type').val("Concepts");
} else if ($.cookie('filter') == "States") {
    $('select#filter_type').val("States");
}

$('select#filter_type').change(function() {
  if ($(this).val() == "All") {
      $('div#context tr.C, tr.S').show();
      $.cookie('filter', null);
  }
  else if ($(this).val() == "Concepts") {
      $('div#context tr.C').show();
      $('div#context tr.S').hide();
      $.cookie('filter', 'Concepts');
  }
  else if ($(this).val() == "States") {
        $('div#context tr.C').hide();
        $('div#context tr.S').show();
        $.cookie('filter', 'States');
  }
  
}).change();

});
</script>

{% if message %}
  <div id="error_box">{{ message|safe }}</div>
{% endif %}
  <div id="header">
    {% if user.is_authenticated %}
		  {{ user.username }}
		  <a href="{% url exploration.views.logout_view %}">log out</a>
		{% else %}
		  <a href="{% url django.contrib.auth.views.login %}?next={% url exploration.views.main %}">log in</a>
		{% endif %}
		&nbsp;
		<a href="{% url exploration.views.log %}">History</a>
		&nbsp;
		Display
		<select id="filter_type" name="filter">
		    <option>All</option>
		    <option>Concepts</option>
		    <option>States</option>
		</select>
  </div>
  <h1>Context</h1>
	<div id="context">
	  <form action="/newobject" method="post">
	  {% csrf_token %}
	  <input type="hidden" id="hidden_imp_pk" name="imp_pk" value="">
	  <table>
      <tr>
        <th></th>
          {% for attribute in attributes %}
          <th>
            <a href="attributes/edit/{{ attribute.pk }}" title="Edit Attribute">{{ attribute.name }}</a>
          </th>
          {% endfor %}
      </tr>
	    {% for object in objects %}
		  <tr class="{{ object.type }}">
		    <td class="header"><a href="objects/edit/{{ object.pk }}" title="Edit Object">{{ object.name }}<a></td>
			  {% for value in object.get_as_boolean_list %}
			    <td class="cross">
  			  {% if value %}X{% endif %}
  			  </td>
			  {% endfor %}
		  </tr>
      {% endfor %}
      <tr id="new_object" style="display:none;">
          <td><input type="submit" name='save' value="Save Object"/><br><input type="text" name="Name" value="Counterexample"><br><a href="/">Cancel</a></td>
          {% for attribute in attributes %}
            <td class="cross"><input type="checkbox" class="new_obj_checkboxes" name="{{ attribute.name }}"><br>{{ attribute.name }}</td>
          {% endfor %}
      </tr>
    </table>
    </form>
	</div>
	<div id="context_buttons">
	  <p>
      <a href="objects/new" id="#new_object_link">New Object</a>
      <a href="attributes/new">New Attribute</a>
      <a href="export" target="_blank">Export</a>     
    </p>
	</div>
	<div id="exploration">
	  <h1 id="background_title">Background Knowledge</h1>
	  <div id="confirmed_attribute_implications">
	    {% for implication in confirmed_implications %}
	    <div class="implication">
	      {{ implication.get_formatted_premise }}
  	    &rarr;
  	    {{ implication.get_formatted_conclusion }}
  	    <div class="imp_controls">
  	      <a href="" class="aimp_reject" id="{{ implication.pk }}">Reject</a>
  	    </div>
	    </div>
	    {% endfor %}
	  </div>
	  <h1 id="open_title">Open Implications</h1>
	  <div id="open_attribute_implications">
	    {% for implication in open_implications %}
	    <div class="implication">
  	    {{ implication.get_formatted_premise }}
  	    &rarr;
  	    {{ implication.get_formatted_conclusion }}
  	    <div class="imp_controls">
  	      <a href="" class="aimp_accept" id="{{ implication.pk }}">Accept</a>
  	      <a href="" class="aimp_reject" id="{{ implication.pk }}">Reject</a>
  	    </div>
	    </div>
	    {% endfor %}
	  </div>
	</div>
{% endblock %}